// 这个文件会帮我们打包 packages 下的模块，最终打包出js文件



// node dev.js (要打包的名字 -f 打包的格式) === argv.slice(2)


import minimist from "minimist";
import { resolve, dirname } from "path";
import { fileURLToPath } from "url";
import { createRequire } from "module";
import esbuild from "esbuild";

const args = minimist(process.argv.slice(2));
const __filename = fileURLToPath(import.meta.url);  // 获取文件的绝对路径 file: -> /usr
const __dirname = dirname(__filename);              // node中esm模块没有 __dirname
const require = createRequire(import.meta.url);

console.log(__filename, __dirname);


const target = args._[0];           // 打包那个项目
const format = args.f || "iife";    // 打包后的模块化规范

console.log(target, format);

const entry = resolve(__dirname, `../packages/${target}/src/index.ts`);
const pkg = resolve(__dirname, `../packages/${target}/package.json`);

console.log(entry)

esbuild.context({
    entryPoints: [entry],
    outfile: resolve(__dirname, `../packages/${target}/dist/${target}.js`),
    bundle: true,
    platform: "browser",
    sourcemap: true,
    format,
    globalName: pkg.buildOptions?.name
}).then((ctx) => {
    console.log('start dev')
    
    return ctx.watch();
})







